<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="申请时间" prop="newtime">
        <el-date-picker clearable
          v-model="queryParams.newtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="申请人" prop="empName">
        <el-input
          v-model="queryParams.empName"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="审批状态" prop="state">
        <!-- <el-input
          v-model="queryParams.state"
          placeholder="请输入(审批)状态:"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.state"
          placeholder="请选择状态"
          clearable
          @keyup.enter.native="handleQuery"
        ><el-option
              v-for="item in states"
              :key="item.state"
              :label="item.stateName"
              :value="item.state">
            </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="签字" prop="signature">
        <el-input
          v-model="queryParams.signature"
          placeholder="请输入签字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" round @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" round @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['audit:approve:add']"
        >新增</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['audit:approve:edit']"
        >修改</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small" round
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['audit:approve:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="small" round
          @click="handleExport"
          v-hasPermi="['audit:approve:export']"
        >导出</el-button>
      </el-col>
      <el-badge :value="this.totalTwo" class="item">
        <el-button @click="dialogTableVisible = true" style="margin-top: 0px;" size="small" round >待审核信息</el-button>
      </el-badge>
      <el-button type="danger" size="small" round @click="handleRollback" style="margin-left: 20px; margin-top: -10px;" >回滚</el-button>
      <el-dialog width="70.3%" title="待审核信息" :visible.sync="dialogTableVisible">
  <el-table :data="gridData">
    <el-table-column property="empName" label="申请人" width="150"></el-table-column>
    <el-table-column property="newtime" label="申请时间" width="200"></el-table-column>
    <el-table-column property="applyforType" label="审批类别" width="150"></el-table-column>
    <el-table-column property="content" label="审批内容" width="200"></el-table-column>
    <el-table-column label="审批人" width="200">




      <template v-slot="{ row }">
      <el-input v-model="arr[row.serialNumber]" placeholder="审批人签字" @blur="blur(row.serialNumber)"  />
    </template>



    </el-table-column>
    <el-table-column width="250">
      <template v-slot="{ row }">
      <el-button type="primary" @click="okAudit(row.serialNumber)">同意审批</el-button>
      <el-button type="danger" @click="noAudit(row.serialNumber)">拒绝审批</el-button>
    </template>
    </el-table-column>

    <!-- <el-table-column property="address" label="地址"></el-table-column> -->
  </el-table>

</el-dialog>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="approveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="serialNumber" />
      <el-table-column label="申请时间" align="center" prop="newtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.newtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="empName" />


      <el-table-column label="状态" align="center" prop="state" >
      <!-- <template slot-scope="scope">
            {{ scope.row.state==0?'未审核':'' }}
            {{ scope.row.state==1?'审核中':'' }}
            {{ scope.row.state==2?'审核通过':'' }}
            {{ scope.row.state==3?'审核未通过':'' }}
        </template> -->
        <template slot-scope="scope">
          <el-tag  v-if="scope.row.state === 2">审核通过</el-tag>
          <el-tag type="warning" v-if="scope.row.state === 1">审核中</el-tag>
          <el-tag type="danger" v-if="scope.row.state === 3">审核未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请类别" align="center" prop="applyforType" />
      <!-- <el-table-column label="审批内容" align="center" prop="content" /> -->
      <el-table-column label="审批内容(点击查看详情)" align="center" prop="content" >
<!--        内容做详情提示-->
        <template slot-scope="scope">
          <el-button type="text" @click="openNotice(scope)">{{scope.row.content}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="签字" align="center" prop="signature" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['audit:approve:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['audit:approve:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改审批管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请时间" prop="newtime">
          <el-date-picker clearable
            v-model="form.newtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请人" prop="empId">
          <el-input v-model="form.empId" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="(删除)状态:默认0(0:存在,1:删除)" prop="version">
          <el-input v-model="form.version" placeholder="请输入(删除)状态:默认0(0:存在,1:删除)" />
        </el-form-item>
        <el-form-item label="(审批)状态:[0:已通过,1,正在审批,2,未通过,]" prop="state">
          <el-input v-model="form.state" placeholder="请输入(审批)状态:[0:已通过,1,正在审批,2,未通过,]" />
        </el-form-item>
        <el-form-item label="审批内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="签字" prop="signature">
          <el-input v-model="form.signature" placeholder="请输入签字" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApprove, getApprove, delApprove, addApprove, updateApprove, listEmployee, listApproveTwo, updateOkApprove, updateNoApprove, rollback } from "@/api/audit/approve";

export default {
  name: "Approve",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 已审批数据总条数
      total: 0,
      // 未审批数据总条数
      totalTwo: 0,
      // 审批管理表格数据
      approveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        newtime: null,
        empId: null,
        state: null,
        signature: null
      },
      // 表单参数
      form: {},

      arr:[],


      states:[{state:2,stateName:"审核通过"},{state:3,stateName:"审核未通过"}],

      // 审批修改内容
      Audit:{
        serialNumber: null,
        newtime: null,
        empId: null,
        empName: null,
        state: null,
        signature: null,
        remark: null,
        version: null,
        applyforType: null,
        content: null,
      },

      gridData: [],
      // signature:[{signature:"高启强"},{signature:"唐小龙"},{signature:"唐小虎"}],
      // signature:'高启强唐小龙唐小虎',
       signature:['高启强','唐小龙','唐小虎'],

      dialogTableVisible: false,
      // 表单校验
      rules: {
        signature: [
          { required: true, message: "签字人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    is(serialNumber){
      let a =  this.arr[serialNumber]
      // for (let i = 0; i < this.gridData.length; i++) {
      //   if ( this.gridData[i].serialNumber == serialNumber ) {
      //       console.log(this.gridData[i]);
      //   }
      // }
      // console.log(a);
    },


    /** 公告显示详情按钮 */
    openNotice(scope) {
      this.$alert(scope.row.content, scope.row.applyforType+' 的内容详情', {
        confirmButtonText: '确定',
        callback: action => {
          this.$message({
            type: 'info',
            message: scope.row.applyforType+` 的内容详情关闭`
          });
        }
      });
    },
    blur(serialNumber){
      if(!(this.signature.includes(this.Audit.signature))){
        this.$message.error('审核人不能为空或审核人错误!');
      }
    },

    /**审批同意 */
    okAudit(serialNumber){
      this.Audit.signature =  this.arr[serialNumber]
      this.Audit.serialNumber =  serialNumber
      // this.blur()
      if(  this.signature.includes(this.Audit.signature)   ){
     updateOkApprove(this.Audit).then(response => {
              this.$modal.msgSuccess("审批成功");
              this.open = false;
              this.getList();
            });
      }else{
        this.$message.error('审核人不能为空或审核人错误!');
      }
    },
    noAudit(serialNumber){
      this.Audit.signature =  this.arr[serialNumber]
      this.Audit.serialNumber =  serialNumber
      if(  this.signature.includes(this.Audit.signature)   ){
        updateNoApprove(this.Audit).then(response => {
              this.$modal.msgSuccess("已拒绝审批");
              this.open = false;
              this.getList();
            });
      }else{
        this.$message.error('审核人不能为空或审核人错误!');
      }
      // updateNoApprove(this.Audit).then(response => {
      //         this.$modal.msgSuccess("已拒绝审批");
      //         this.open = false;
      //         this.getList();
      //       });
    },

    /** 查询审批管理列表 */
    getList() {
      this.loading = true;
      listApprove(this.queryParams).then(response => {
        this.approveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listEmployee().then(response => {
      this.employeeList = response.rows;
      this.loading = false;
      });
      listApproveTwo().then(response => {
        this.gridData = response.rows;
        this.totalTwo = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serialNumber: null,
        newtime: null,
        empId: null,
        version: null,
        state: null,
        applyforType: null,
        content: null,
        signature: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serialNumber)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审批管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const serialNumber = row.serialNumber || this.ids
      getApprove(serialNumber).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改审批管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serialNumber != null) {
            updateApprove(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApprove(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serialNumbers = row.serialNumber || this.ids;
      this.$modal.confirm('是否确认删除审批管理编号为"' + serialNumbers + '"的数据项？').then(function() {
        return delApprove(serialNumbers);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('audit/approve/export', {
        ...this.queryParams
      }, `approve_${new Date().getTime()}.xlsx`)
    },
    handleRollback(){
      rollback().then((res)=>{
        if (!res){
          this.$message({
            message: '目前不存在撤销功能',
            type: 'warning'
          });
        }else {
          this.getList();
        }
      })
    }
  }
};
</script>


